﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;


#region 文件信息
/****************************************************************
*	公司名称：福建力和行智能科技有限公司
*   作    者：Jeff
*   CLR 版本：4.0.30319.42000
*   创建时间：12/20/2024 10:28:49 AM
*	用 户 名：zheqichen 
*   描述说明：
*
*   修改历史：
*		1）	修 改 人：
*			修改日期：
*			修改内容：			
*
*****************************************************************/
#endregion

namespace Lhxzn.LangExtractor
{
    /// <summary>
    /// 类    名:FileSelectTextBox
    /// 描    述:
    /// 修改时间:12/20/2024 10:28:49 AM
    /// </summary>
    public class FileSelectTextBox:UserControl
    {
        #region 成员变量

        #region private



        #endregion


        #region protected
        protected Button _FolderButton=new Button();
        protected TextBox _TextBox=new TextBox();

        #endregion


        #region public
        [Browsable(true)]
        [Category("FileSelectTextBox")]
        [Description("是否选择文件夹")]
        public bool IsSelectFolder { get; set; }

        [Browsable(true)]
        [Category("FileSelectTextBox")]
        [Description("选择文件的文件过滤")]
        public string FileSelectFilter { get; set; }

        [Browsable(true)]
        [Category("FileSelectTextBox")]
        [Description("是否允许输入文件路径")]
        public bool InputEnabled
        {
            get
            {
                return !_TextBox.ReadOnly;
            }
            set
            {
                _TextBox.ReadOnly = !value;
            }
        }

        [Browsable(true)]
        [Category("FileSelectTextBox")]
        [Description("选中的文件或文件夹路径")]
        public string SelectedFile
        {
            get
            {
                return _TextBox.Text;
            }
            set
            {
                _TextBox.Text = value;
            }
        }

        [Category("FileSelectTextBox")]
        [Description("是否检测文件是否存在")]
        public bool CheckFileExists { get; set; }

        public new string Text
        {
            get
            {
                return SelectedFile;
            }
            set
            {
                SelectedFile = value;
            }
        }

        public new event EventHandler TextChanged;

        public event EventHandler SelectedFileChanged;
        #endregion

        #endregion


        #region 构造函数
        /// <summary>
        /// 函 数 名:构造函数
        /// 函数描述:默认构造函数
        /// 修改时间:12/20/2024 10:28:49 AM
        /// </summary>
        public FileSelectTextBox()
        {
            _FolderButton.Text = string.Empty;
            CheckFileExists = true;
            IsSelectFolder = false;
            InputEnabled = false;
            FileSelectFilter = "All Files(*.*)|*.*";
            base.Controls.Add(_FolderButton);
            base.Controls.Add(_TextBox);
            _FolderButton.Size = new Size(32, 32);
            _FolderButton.Text = "...";
            _TextBox.Location = new Point(0, 0);
            base.SizeChanged += FileSelectTextBox_SizeChanged;
            FileSelectTextBox_SizeChanged(this, null);
            _FolderButton.MouseClick += OnFolderButtonMouseClick;
            _FolderButton.MouseDoubleClick += OnFolderButtonMouseDoubleClick;
            _TextBox.TextChanged += _TextBox_TextChanged;
            _TextBox.KeyDown += OnTextBoxKeyDown;
            _TextBox.DoubleClick += _TextBox_DoubleClick;
        }
        #endregion

        #region 父类函数重载、接口实现

        #endregion

        #region 函数

        #region private
        private void OnTextBoxKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return && InputEnabled)
            {
                this.SelectedFileChanged?.Invoke(this, new EventArgs());
            }
        }

        private void OnFolderButtonMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                _TextBox.Clear();
            }
        }

        private void OnFolderButtonMouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            if (IsSelectFolder)
            {
                using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
                {
                    folderBrowserDialog.SelectedPath = SelectedFile;
                    if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                    {
                        SelectedFile = folderBrowserDialog.SelectedPath;
                        this.SelectedFileChanged?.Invoke(this, new EventArgs());
                    }

                    return;
                }
            }

            using OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.FileName = SelectedFile;
            openFileDialog.CheckFileExists = CheckFileExists;
            openFileDialog.Filter = FileSelectFilter;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                SelectedFile = openFileDialog.FileName;
                this.SelectedFileChanged?.Invoke(this, new EventArgs());
            }
        }

        private void _TextBox_DoubleClick(object sender, EventArgs e)
        {
            Process.Start("explorer", "/select,\"" + Text + "\"");
        }

        private void _TextBox_TextChanged(object sender, EventArgs e)
        {
            if (this.TextChanged != null)
            {
                this.TextChanged(this, e);
            }
        }

        private void FileSelectTextBox_SizeChanged(object sender, EventArgs e)
        {
            if (_FolderButton.Height > _TextBox.Height)
            {
                base.Height = _FolderButton.Height;
            }
            else
            {
                base.Height = _TextBox.Height;
            }

            _FolderButton.Left = base.Width - _FolderButton.Width;
            _FolderButton.Top = (base.Height - _FolderButton.Height) / 2;
            _TextBox.Top = (base.Height - _TextBox.Height) / 2;
            _TextBox.Width = _FolderButton.Left - 5;
        }


        #endregion


        #region protected



        #endregion


        #region public


        #endregion

        #endregion
    }
}
